var app = getApp();
var common = require('../../utils/util.js');
var pageIndex = 1;
var step=10;
var pageSize = step;
var hasNext;
var arr = [];
Page({

  /**
   * 页面的初始数据
   */
  data: {
    search:"",
    typeList:[],
    bannerList:[],
    mallList:[],
    typeId:-1,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    pageIndex = 1;
    hasNext=false;
    arr = [];
    var that=this;
    //商品类型
    wx.request({
      url: app.getUrl() + '/api/mall/goods_type',
      success(res) {
        if (res && res.statusCode == 200 && res.data && res.data.status == 200) {
          res.data.obj.unshift({typeId: "", typeName: "首页", isDeleted: "00"})
          that.setData({
            typeList:res.data.obj,
            typeId:res.data.obj[0].typeId
          })
          that.getInfo()
        }
      },
      fail(res) {
        console.log(res)
        wx.showToast({
          title: '连接服务器失败',
          icon: 'none',
          duration: 2000
        })
      }
    })
    //banner列表
    wx.request({
      url: app.getUrl() + '/api/mall/banner_list',
      success(res) {
        if (res && res.statusCode == 200 && res.data && res.data.status == 200) {
          that.setData({
            bannerList:res.data.obj
          })
        }
      },
      fail(res) {
        console.log(res)
        wx.showToast({
          title: '连接服务器失败',
          icon: 'none',
          duration: 2000
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(hasNext){
      pageIndex += 1;
      this.getInfo()
    }else{
      wx.showToast({
        title: '暂无更多数据',
        icon: 'none',
        duration: 2000
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  selectType(e){
    this.setData({
      typeId:e.target.dataset.id
    })
    pageIndex = 1;
    arr = [];
    this.getInfo();
  },
  inputChange(e){
    this.setData({
      search:e.detail.value
    })
  },
  search(){
    pageIndex = 1;
    arr = [];
    this.getInfo()
  },
  detail(e){
    if(app.globalData.customerId){
      wx.navigateTo({
        url: '../mallShoppingDetail/mallShoppingDetail?giftid='+e.target.dataset.giftid,
      })
    }else{
      wx.showToast({
        title: '请您至VIP通道登录',
        icon:'none',
        duration:3000
      })
    }
  },
  getInfo(){
    wx.showLoading({
      title: '加载中',
    })
    var that = this;
    wx.request({
      url: app.getUrl() + '/api/mall/goods_page',
      data:{
        goodsTypeId:this.data.typeId,
        goodsName:this.data.search,
        pageIndex:pageIndex,
        pageSize:pageSize,
      },
      success(res) {
        if (res && res.statusCode == 200 && res.data && res.data.status == 200) {
          wx.hideLoading({
            success: (res) => {},
          })
          hasNext = res.data.obj.hasNext;
          for(let i=0;i<res.data.obj.rows.length;i++){
            if(res.data.obj.rows[i].goodsSubscribeInfo){
              res.data.obj.rows[i].goodsSubscribeInfo.goodsSubscribeStarttime=res.data.obj.rows[i].goodsSubscribeInfo.goodsSubscribeStarttime.split("-")[1]+'月'+res.data.obj.rows[i].goodsSubscribeInfo.goodsSubscribeStarttime.split("-")[2].split(" ")[0]+'日'+res.data.obj.rows[i].goodsSubscribeInfo.goodsSubscribeStarttime.split("-")[2].split(" ")[1].split(":")[0]+'时'+res.data.obj.rows[i].goodsSubscribeInfo.goodsSubscribeStarttime.split("-")[2].split(" ")[1].split(":")[1]+'分';
              
              res.data.obj.rows[i].goodsSubscribeInfo.goodsSubscribeEndtime=res.data.obj.rows[i].goodsSubscribeInfo.goodsSubscribeEndtime.split("-")[1]+'月'+res.data.obj.rows[i].goodsSubscribeInfo.goodsSubscribeEndtime.split("-")[2].split(" ")[0]+'日'+res.data.obj.rows[i].goodsSubscribeInfo.goodsSubscribeEndtime.split("-")[2].split(" ")[1].split(":")[0]+'时'+res.data.obj.rows[i].goodsSubscribeInfo.goodsSubscribeEndtime.split("-")[2].split(" ")[1].split(":")[1]+'分';
            }
          }
          arr = arr.concat(res.data.obj.rows);
          if(res.data.obj.total == 0 ){
            wx.showToast({
              title: '暂无商品',
              icon: 'none',
              duration: 2000
            })
          }
          console.log(arr)
          that.setData({
            mallList:arr,
          })
        }
      },
      fail(res) {
        console.log(res)
        wx.showToast({
          title: '连接服务器失败',
          icon: 'none',
          duration: 2000
        })
      }
    })
  },
  buy(e){
    if(app.globalData.customerId){
      wx.navigateTo({
        url: '../mallShoppingDetail/mallShoppingDetail?giftid='+e.currentTarget.dataset.giftid,
      })
    }else{
      wx.showToast({
        title: '请您至VIP通道登录',
        icon:'none',
        duration:3000
      })
    }
  },
})